<template>
  <BasicDrawer
    v-bind="$attrs"
    @register="registerDrawer"
    showFooter
    :title="getTitle"
    width="50%"
    @ok="handleSubmit"
  >
    <BasicForm @register="registerForm" />
  </BasicDrawer>
</template>
<script lang="ts">
  import { defineComponent, ref, computed, unref } from 'vue';
  import { BasicForm, useForm } from '/@/components/Form/index';
  import { formSchema } from './menu.data';
  import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';

  import { getMenuList } from '/@/api/demo/system';
  import { createMenu, updateMenu } from '/@/api/sys/menu';
  import {message} from 'ant-design-vue'
  export default defineComponent({
    name: 'MenuDrawer',
    components: { BasicDrawer, BasicForm },
    emits: ['success', 'register'],
    setup(_, { emit }) {
      const isUpdate = ref(true);

      const [registerForm, { resetFields, setFieldsValue, updateSchema, validate }] = useForm({
        labelWidth: 100,
        schemas: formSchema,
        showActionButtonGroup: false,
        baseColProps: { lg: 12, md: 24 },
      });
      let treeDataAll: any;
      const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
        resetFields();
        setDrawerProps({ confirmLoading: false });
        isUpdate.value = !!data?.isUpdate;

        if (unref(isUpdate)) {
          setFieldsValue({
            ...data.record,
          });
        }
        const treeData = await getMenuList();
        treeDataAll = treeData;
        updateSchema({
          field: 'parentMenu',
          componentProps: { treeData },
        });
      });

      const getTitle = computed(() => (!unref(isUpdate) ? '新增菜单' : '编辑菜单'));
      const chckIfAllChildrenDisabled = (updateMenu) => {
        const id = updateMenu.id;
        let isAllClose = true;
        //查询二级菜单
        const submenu = treeDataAll.filter((item) => item.pid === id);

        submenu.forEach((item) => {
          if (+item.actvie === 1) {
            isAllClose = false;
            return 
          }
        });

        if (!isAllClose) {
          return false;
        }
        //查询三级菜单
        if (submenu.length > 0) {
          submenu.forEach((item) => {
            isAllClose = chckIfAllChildrenDisabled(item);
            if (!isAllClose) {
              return false;
            }
          });
        }

        return isAllClose;
      };
      async function handleSubmit() {
        try {
          const values = await validate();
          setDrawerProps({ confirmLoading: true });
          // TODO custom api
          console.log(values);
          if (values.parentMenu) {
            values.pid = values.parentMenu;
          } else {
            values.pid = 0;
          }
          Reflect.deleteProperty(values, 'parentMenu');
          const isUpdateForm = unref(isUpdate);
          if (isUpdateForm) {
            const id = treeDataAll.find((item) => item.name === values.name).id;
            values.id = id;
            const res=chckIfAllChildrenDisabled(values);
            console.log(res,'res');
            
            if(res){
              await updateMenu({ data: values });
            }else{
              message.warn('请先关闭所有子菜单');
            }
            
          } else {
            await createMenu({ data: values });
          }

          closeDrawer();
          emit('success');
        } finally {
          setDrawerProps({ confirmLoading: false });
        }
      }

      return { registerDrawer, registerForm, getTitle, handleSubmit };
    },
  });
</script>
